package com.example.openguass.controller;


import com.example.openguass.entity.Category;
import com.example.openguass.entity.CommonResult;
import com.example.openguass.entity.Customer;
import com.example.openguass.service.CustomerService;
import com.example.openguass.validation.Update;
import com.example.openguass.vo.CategoryPageVO;
import com.example.openguass.vo.CategoryVO;
import com.example.openguass.vo.CustomerPageVO;
import com.example.openguass.vo.CustomerVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xa
 * @since 2022-11-17
 */
@RestController
@CrossOrigin
@RequestMapping("/customer")
@Api(value = "CustomerController", tags = "用户接口")
public class CustomerController {
    private CustomerService customerService;
    @Autowired
    private void setCustomerService(CustomerService customerService){
        this.customerService = customerService;
    }

    @PostMapping("insert")
    public CommonResult insert(@RequestBody CustomerVO customerVO){
        CommonResult result = new CommonResult().init();
        if (customerService.exit(customerVO.getLoginname())){
            return (CommonResult) result.failCustom(-10086,"当前用户已存在");
        }
        if (customerService.insert(customerVO)){
            return result.success("创建用户成功",customerVO);
        }else {
            return (CommonResult) result.failCustom(-10086,"创建用户失败");
        }
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody @Validated({Update.class}) CustomerVO customerVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            return (CommonResult) result.failIllegalArgument(bindingResult.getFieldErrors()).end();
        }
        if (customerService.getById(customerVO.getId()) == null){
            return (CommonResult) result.failCustom(-10086,"不存在当前用户");
        }
        if (customerService.update(customerVO)){
            return result.success("CustomerVO",customerVO);
        }else {
            return (CommonResult) result.failCustom(-10086,"更新用户失败");
        }
    }

    @GetMapping("/delete/{customerId}")
    public CommonResult delete(@PathVariable String customerId){
        CommonResult result = new CommonResult().init();
        if (customerService.getById(customerId) == null){
            return (CommonResult) result.failCustom(-10086,"该用户不存在");
        }
        if (customerService.removeById(customerId)){
            return (CommonResult) result.success();
        }else {
            return (CommonResult) result.failCustom(-10086,"删除用户失败");
        }
    }

    @GetMapping("/select/{customerId}")
    public CommonResult select(@PathVariable String customerId){
        CommonResult result = new CommonResult().init();
        if (customerService.getById(customerId) == null){
            return (CommonResult) result.failCustom(-10086,"该用户不存在");
        }
        Customer customer = customerService.getById(customerId);
        return result.success("customer",customer);
    }


    @PostMapping("findPage")
    public CommonResult findPage(@RequestBody CustomerPageVO customerPageVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            result.failIllegalArgument(bindingResult.getFieldErrors()).end();
            return result;
        }
        return (CommonResult) result.success("page",customerService.findPage(customerPageVO)).end();
    }
}
